package com.myjob.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.myjob.config.Constants;
import com.myjob.entity.HuntingStatus;
import com.myjob.entity.User;
import com.myjob.exception.OptionException;
import com.myjob.response.Result;
import com.myjob.service.HuntingStatusService;
import com.myjob.utils.UtilValidate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @author Chen Yuan
 * @create 2021-01-27  17:08
 */
@RestController
@Slf4j
@RequestMapping("/jobs")
public class HuntingStatusController {
	@Autowired
	private HuntingStatusService huntingStatusService;

	/**
	 * 分页查询所有状态信息
	 * @param currentPage 当前页
	 * @param pageSize 页大小
	 * @param userName 用户姓名
	 * @param hrName hr 姓名
	 * @return
	 */
	@GetMapping("/huntingInfo")
	public Result selectHuntingStatus(@RequestParam("currentPage") Integer currentPage,
	                                  @RequestParam(value = "pageSize", required = false) Integer pageSize,
	                                  @RequestParam(value = "userName", required = false) String userName,
	                                  @RequestParam(value = "hrName", required = false) String hrName,
	                                  @RequestParam(value = "userId") String userId,
	                                  @RequestParam(value = "searchValue", required = false) String searchValue){
		Result result = null;
		try {
			if (UtilValidate.isEmpty(currentPage)){
				currentPage = 1;
			}
			result = huntingStatusService.selectHuntingStatus(currentPage, pageSize, userName, hrName, userId, searchValue);
		} catch (Exception e) {
			e.printStackTrace();
			throw new OptionException("查询失败");
		}
		return result;
	}

	/**
	 * 查询一条记录的所有信息
	 * @param huntingStatus 状态信息
	 * @return
	 */
	@GetMapping("/huntingInfo/detail")
	public Result selectOneHunting(@RequestBody HuntingStatus huntingStatus){
		Result result = null;
		try {
			if (UtilValidate.isEmpty(huntingStatus)){
				result = Result.failed("参数为空");
			}else {
				result = huntingStatusService.selectOneHunting(huntingStatus);
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new OptionException("查询失败");
		}
		return result;
	}

	/**
	 * 新增
	 * @param huntingStatus 状态信息
	 * @return
	 */
	@PutMapping("/huntingInfo")
	public Result addHunting(@RequestBody HuntingStatus huntingStatus){
		Result result = null;
		try {
			if (UtilValidate.isEmpty(huntingStatus)){
				result = Result.failed("参数为空");
			}else {
				int insert = huntingStatusService.addHunting(huntingStatus);
				if (insert == 1){
					result = Result.succeed("记录 添加 成功");
				}else {
					result = Result.failed("记录 添加 失败");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new OptionException("记录 添加 失败");
		}
		return result;
	}

	/**
	 * 修改状态
	 * @param huntingStatus 状态信息
	 * @return
	 */
	@PostMapping("/huntingInfo")
	public Result updateHuntingStatus(@RequestBody HuntingStatus huntingStatus){
		Result result = null;
		try {
			if (UtilValidate.isEmpty(huntingStatus)){
				result = Result.failed("参数为空");
			}else {
				int update = huntingStatusService.updateHuntingStatus(huntingStatus);
				if (update == 1){
					result = Result.succeed("记录 修改 成功");
				}else {
					result = Result.failed("记录 修改 失败");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new OptionException("记录 修改 失败");
		}
		return result;
	}

	/**
	 * 删除
	 */
	@DeleteMapping("/huntingInfo")
	public Result deleteHunting(@RequestParam("huntingId") String huntingId,
	                         @RequestParam(value = "deleted") Integer deleted){
		Result result = null;
		try {
			if (UtilValidate.isEmpty(huntingId)){
				result = Result.failed("参数为空");
			}else {
				deleted = UtilValidate.isEmpty(deleted) ? Constants.getNoDel() : deleted;
				int delete = huntingStatusService.deleteHunting(huntingId, deleted);
				if (delete == 1){
					result = Result.succeed("记录 删除 成功");
				}else {
					result = Result.failed("记录 删除 失败");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new OptionException("记录 删除 失败");
		}
		return result;
	}

	/**
	 * 查询所有候选人信息
	 * @param currentPage
	 * @param pageSize
	 * @param searchValue
	 * @param userId
	 * @return
	 */
	@GetMapping("/selectHuntingUsers")
	public Result selectHuntingUsers(@RequestParam("currentPage") Integer currentPage,
	                                 @RequestParam(value = "pageSize", required = false) Integer pageSize,
	                                 @RequestParam(value = "searchValue", required = false) String searchValue,
	                                 @RequestParam(value = "userId", required = false) String userId){
		Result result = null;
		try {
			result = huntingStatusService.selectHuntingUsers(currentPage, pageSize, searchValue, userId);
		} catch (Exception e) {
			e.printStackTrace();
			throw new OptionException("查询求职情况及求职者信息失败");
		}
		return result;
	}

	/**
	 * 查询感兴趣的求职者列表
	 * @param userId
	 * @param huntingStatus
	 * @return
	 */
	@GetMapping("/getListByStatus")
	public Result getListByStatus(@RequestParam(value = "userId") String userId,
	                              @RequestParam(value = "huntingStatus") String huntingStatus){
		Result result = null;
		try {
			List<String> statusList = Arrays.asList(huntingStatus.split(","));

			QueryWrapper<HuntingStatus> wrapper = new QueryWrapper<>();
			wrapper.and(w -> w.eq("company_id", userId)
							.or().eq("hr_id", userId)
							.or().eq("user_id", userId));
			wrapper.in("hunting_status", statusList);
			List<HuntingStatus> list = huntingStatusService.list(wrapper);

			ArrayList<String> huntingList = new ArrayList<>();

			for (HuntingStatus status : list) {
				huntingList.add(status.getHuntingId());
			}

			result = Result.succeed(huntingList);
		}catch (Exception e){
			e.printStackTrace();
			throw new OptionException("查询求职申请列表失败");
		}
		return result;
	}

	/**
	 * 按照编号查询申请状态
	 * 查询是否投递过该岗位
	 * @param userId
	 * @param hrId
	 * @param companyId
	 * @param jobId
	 * @return
	 */
	@GetMapping("/selectHuntingByIds")
	public Result selectHuntingByIds(@RequestParam(value = "userId", required = false) String userId,
									 @RequestParam(value = "hrId", required = false) String hrId,
									 @RequestParam(value = "companyId", required = false) String companyId,
									 @RequestParam(value = "jobId", required = false) String jobId){
		Result result = null;
		try {
			result = huntingStatusService.selectHuntingByIds(userId, hrId, companyId, jobId);
		}catch (Exception e){
		    e.printStackTrace();
		    throw new OptionException("查询失败");
		}
		return result;
	}

	@GetMapping("/countHunting")
	public Result countHunting() {
		Result result = null;
		try {
			result = huntingStatusService.countHunting();
		} catch (Exception e) {
			e.printStackTrace();
			throw new OptionException("查询失败");
		}
		return result;
	}
}
